﻿<%@ Page Language="C#" theme="Register" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["CaptchaImageText"] = GenerateRandomCode();
            ListItem defaultlist = new ListItem();
            defaultlist.Text = Resources.faContent.membershipDropDownListDefaultItem;
            defaultlist.Value = "0";
        }
        
        Page.Title = Resources.faContent.membershipPageTitle;
        lFormNote.Text = Resources.faContent.membershipLFromNote;
        lLabelEmail.Text = Resources.faContent.membershipLEmail;
        lLabelPassword.Text = Resources.faContent.membershipLPassword;
        lLabelRepeatPassword.Text = Resources.faContent.membershipLRepeatPassword;
        lLabelFirstName.Text = Resources.faContent.membershipLFirstName;
        lLabelLastName.Text = Resources.faContent.membershipLLastName;
        lLabelNumImage.Text = Resources.faContent.membershipLNumImage;
        lLabelCaptchaLink.Text = Resources.faContent.membershipLCaptchaLink;
        
        btnForm.Text = Resources.faContent.membershipBText;
        rfvCaptcha.ErrorMessage = Resources.faContent.membershipRFVCaptchaErrorMsg;
        revCaptcha.ErrorMessage = Resources.faContent.memershipREVCaptchaErrorMsg;
        revEmail.ErrorMessage = Resources.faContent.membershipREVEmailErrorMeg;
        rfvEmail.ErrorMessage = Resources.faContent.membershipRFVDefaultMsg;
        rfvFirstName.ErrorMessage = Resources.faContent.membershipRFVDefaultMsg;
        rfvLastName.ErrorMessage = Resources.faContent.membershipRFVDefaultMsg;
        rfvPassword.ErrorMessage = Resources.faContent.membershipRFVDefaultMsg;
        rfvRepeatPassword.ErrorMessage = Resources.faContent.membershipRFVDefaultMsg;
        cvRepeatPassword.ErrorMessage = Resources.faContent.membershipCVRepeatPasswordErrorMsg;
        cvEmail.ErrorMessage = Resources.faContent.memberhsipCVEmailErrorMsg;
        lPersonalInfo.Text = Resources.faContent.membershipLPersonalInfo;
        
        lPoint1.Text = Resources.faContent.membershipLPont1;
        lPoint2.Text = Resources.faContent.membershipLPont2;
        lPoint3.Text = Resources.faContent.membershipLPont3;
        lPoint4.Text = Resources.faContent.membershipLPont4;
        lPoint5.Text = Resources.faContent.membershipLPont5;
        lPoint6.Text = Resources.faContent.membershipLPont6;
        
    }
    private string GenerateRandomCode()
    {
        string s = "";
        Random rand = new Random();
        for (int i = 0; i <= 5; i++)
        {
            s = s + rand.Next(0, 10).ToString();
        }
        return s;
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        string NoBotCode = Session["CaptchaImageText"].ToString();
        try
        {
            if (string.Compare(NoBotCode, tbNumImage.Text.Trim()) == 0)
            {
                Membership.CreateUser(tbEmail.Text, tbPassword.Text, tbEmail.Text);
                ProfileCommon pf = (ProfileCommon)ProfileCommon.Create(tbEmail.Text);
                pf.Register.FirstName = tbFirstName.Text;
                pf.Register.LastName = tbLastName.Text;
                pf.Register.SendEmail = true;
                pf.Save();

                Roles.AddUserToRole(tbEmail.Text, Resources.global.NormalUserRole);
                MembershipUser user = Membership.GetUser(tbEmail.Text);
                user.IsApproved = false;
                Membership.UpdateUser(user);
                StreamReader sreader = new StreamReader(Server.MapPath("~") + Resources.global.EmailsFilesRoot + "Activation.htm");
                string content = sreader.ReadToEnd();
                string actLink = Resources.global.root + "Activation.aspx?id=" + user.ProviderUserKey.ToString();
                content = content.Replace("{LinkAddress}", actLink);
                content = content.Replace("{Name}", findUser(tbEmail.Text));
                SendEmail.EmailActivation(tbEmail.Text, content, Resources.global.ActivationEmailTitle, Resources.global.SMTP, Resources.global.EmailPass, Resources.global.EmailAddress);
                sreader.Close();
                sreader.Dispose();
                Response.Redirect("sendEmailMessage.htm");
            }
            else
            {
                lMessage.ForeColor = System.Drawing.Color.Red;
                lMessage.Text = Resources.faContent.membershipLMessageFail; 
            }
        }
        catch (MembershipCreateUserException ex)
        {
            lMessage.ForeColor = System.Drawing.Color.Red;
            lMessage.Text = Resources.faContent.membershipLMessageCreateUserError;
        }
            
    }
    private string findUser(string email)
    {
        ProfileCommon pf = Profile.GetProfile(email);
        string  str = pf.Register.FirstName + " " + pf.Register.LastName;
        return str;
    }

    [System.Web.Services.WebMethod] 
    public static bool UserValidation(string Email)
    {
       return Membership.GetUser(Email) != null;
    }
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="script/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="script/popupWindow.js" type="text/javascript"></script>
<script type="text/javascript"> 
$(function()
{
$('.captchalink').popupWindow({ 
height:600, 
width:540, 
top:50, 
left:50 
});

}); 

 function check(sender, args) {
         args.IsValid = (args.Value.length >= 4);
     }

function check(sender, args)
{
  //alert(args.IsValid);
  

  $.ajax(
     {
     type: "POST",
     url: "membership.aspx/UserValidation",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     data:"{'Email':'"+ $('#<%=tbEmail.ClientID %>').val() + "'}",
     success: function(msg) 
      {
       if(msg.d==true)
       $('#cvEmail').show();
      },
     failure: function(msg) 
      {
       alert(msg.d);
      }
    });
   
}

</script>
<head runat="server">
<title></title>
</head>
<body>
    <form id="form1" runat="server">
     <div id="form">
        <div class="formSection">
        <div class="header"><span><asp:Literal runat="server" id="lPersonalInfo" /></span></div>
        <div class="content">
        <div id="mLogo">
        <div class="mlogoimg"></div>
        
         <p>
         <asp:Literal runat="server" ID="lFormNote" />
         </p>
         <ul>
          <li>
           <asp:Literal runat="server" ID="lPoint1" />
          </li>
          <li>
           <asp:Literal runat="server" ID="lPoint2" />
          </li>
          <li>
           <asp:Literal runat="server" ID="lPoint3" />
          </li>
          <li>
           <asp:Literal runat="server" ID="lPoint4" />
          </li>
          <li>
           <asp:Literal runat="server" ID="lPoint5" />
          </li>
          <li>
           <asp:Literal runat="server" ID="lPoint6" />
          </li>
         </ul>
        </div>
        <table>
          <tr>
          <td>
           <label><asp:Literal runat="server" ID="lLabelEmail" /></label>
          </td>
          <td >
           <div style="position:relative;">
           <div id="loading" ></div>
           </div>
           <asp:TextBox runat="server" CssClass="dleft" MaxLength="40" ValidationGroup="membership" ID="tbEmail"   />
          </td>
          <td >
           <asp:RequiredFieldValidator   ValidationGroup="membership"  runat="server" ID="rfvEmail" Display="Dynamic"  ControlToValidate="tbEmail"   SetFocusOnError="true" ></asp:RequiredFieldValidator>
           <asp:CustomValidator ValidationGroup="membership" runat="server" ID="cvEmail" Display="Dynamic"  ControlToValidate="tbEmail" SetFocusOnError="true"  ClientValidationFunction="check" EnableClientScript="true" ></asp:CustomValidator><br />
           <asp:RegularExpressionValidator  ValidationGroup="membership"  ID="revEmail" runat="server" Display="Dynamic"  ControlToValidate="tbEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
          </td>
          </tr>
          <tr>
          <td>
          <label><asp:Literal runat="server"  ID="lLabelPassword" /></label>
          </td>
          <td>
          <asp:TextBox runat="server" MaxLength="50"  CssClass="dleft" TextMode="Password" ValidationGroup="membership" ID="tbPassword" />
         </td>
         <td>
          <asp:RequiredFieldValidator ValidationGroup="membership"  runat="server" ID="rfvPassword" Display="Dynamic"  ControlToValidate="tbPassword"   SetFocusOnError="true" ></asp:RequiredFieldValidator>
         </td>
         </tr>
          <tr>
         <td>
          <label><asp:Literal runat="server" ID="lLabelRepeatPassword" /></label>
          </td>
          <td>
          <asp:TextBox runat="server" MaxLength="50"  CssClass="dleft" TextMode="Password" ValidationGroup="membership" ID="tbRepeatPassword"  />
         </td>
         <td>
          <asp:RequiredFieldValidator ValidationGroup="membership"  runat="server" ID="rfvRepeatPassword" Display="Dynamic"  ControlToValidate="tbRepeatPassword"   SetFocusOnError="true" ></asp:RequiredFieldValidator>
          <asp:CompareValidator runat="server" ID="cvRepeatPassword" SetFocusOnError="true" ValidationGroup="membership"  ControlToCompare="tbPassword" ControlToValidate="tbRepeatPassword"   Operator="Equal"   Display="Dynamic" ></asp:CompareValidator>
         </td>
         </tr>
          <tr>
          <td>
           <label><asp:Literal runat="server" ID="lLabelFirstName" /></label>
          </td>
          <td>
          <asp:TextBox runat="server" MaxLength="35" ValidationGroup="membership" ID="tbFirstName"   />
         </td>
         <td>
          <asp:RequiredFieldValidator ValidationGroup="membership"  runat="server" ID="rfvFirstName" Display="Dynamic"  ControlToValidate="tbFirstName"   SetFocusOnError="true" ></asp:RequiredFieldValidator>
         </td>
         </tr>
          <tr>
          <td>
          <label><asp:Literal runat="server" ID="lLabelLastName" /></label>
          </td>
          <td>
          <asp:TextBox runat="server" MaxLength="40" ValidationGroup="membership" ID="tbLastName"   />
         </td>
         <td>
          <asp:RequiredFieldValidator ValidationGroup="membership"  runat="server" ID="rfvLastName" Display="Dynamic"  ControlToValidate="tbLastName"   SetFocusOnError="true" ></asp:RequiredFieldValidator>
         </td>
         </tr>
           <tr>
            <td>
             <label><asp:Literal runat="server" ID="lLabelNumImage" /></label>
            </td>
            <td>
              <center> <a  class="captchalink" href="captcha.htm" ><asp:Literal runat="server" ID="lLabelCaptchaLink" /></a></center>
              <asp:Image ID="imgCaptcha"  style="margin-top:4px;border:solid 1px #999"  runat="server"  ImageUrl="Captcha.aspx"  />
              <br />
              <asp:TextBox   ValidationGroup="membership" runat="server" CssClass="dleft"    ID="tbNumImage"    MaxLength="6" ></asp:TextBox> 
            </td>
           <td>
              <asp:RequiredFieldValidator ValidationGroup="membership"  runat="server" ID="rfvCaptcha" Display="Dynamic"  ControlToValidate="tbNumImage"   SetFocusOnError="true" ></asp:RequiredFieldValidator>
              <asp:RegularExpressionValidator Display="Dynamic"     ControlToValidate="tbNumImage" runat="server" SetFocusOnError="true"  ValidationGroup="membership" ID="revCaptcha" ValidationExpression="^\d{0,9}$" ></asp:RegularExpressionValidator></td>
           </tr>
           <tr>
            <td colspan="2">
             <center>
         <asp:Label runat="server" ID="lMessage" />
         <br /><br />
         <asp:Button CssClass="submitbutton"  OnClick="btn_Click" runat="server" ID="btnForm" ValidationGroup="membership"  />
        </center>
            </td>
           </tr>
        </table>
        </div>
        </div>
        
     </div>
    </form>
</body>
</html>
